Loader.php

<?php

namespace Tlf\User;


trait Loader {
    
    public function userFromEmail($email){
        $model = $this->backend->userFromEmail($email);
        if ($model!=false)return new \Tlf\User($model, $this->backend);
        return null;
    }

    public function getLoggedInUser(){
        try {
            $model = $this->backend->getLoggedInUser();
        } catch (\Exception $e){
            $model = null;
            $this->logoutUser();
        }
        $user = new \Tlf\User($model, $this->backend);
        return $user;
    }
    /**
     * Get a user from a url-slug
     * 
     * @param string $slug a string like `user-USER_ID` where `USER_ID` is an `int`
     * @return boolean|NULL|\Tlf\User - false if the slug is in the wrong format. null, if there is no user for the slug. Otherwise a user object
     */
    public function getUserFromSlug(string $slug){
        $parts = explode('-',$slug);
        if ($parts[0]!='user')return false;
        if ((int)$parts[1]!=$parts[1])return false;
        $model = $this->backend->userById($parts[1]);
        if ($model==null)return null;
        return new \Tlf\User($model, $this->backend);
    }
}